package com.example.demoJDBC.controller;

import com.example.demoJDBC.domain.MysqlDatasource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
public class SpringBootMysqlApplication {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping("insert")
    public String insert() {
        String id = UUID.randomUUID().toString();
        String sql = "insert into mysql_datasource (id,name) values ('" + id + "','小宋')";
        jdbcTemplate.execute(sql);
        return "插入完毕";
    }

    @GetMapping("select")
    public String select() {
        String sql = "select * from mysql_datasource";
        List<MysqlDatasource> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MysqlDatasource.class));
        //查询总记录数
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
        return "查询的数据:" + query.toString();
    }

    @GetMapping("update")
    public String update() {
        return null;
    }


}
























